package com.shigu.test;

import com.opentae.data.mall.beans.MoPassagewayAccount;
import com.opentae.data.mall.examples.MoPassagewayAccountExample;
import com.opentae.data.mall.interfaces.MoPassagewayAccountMapper;
import com.shigu.main4.common.util.MoneyUtil;
import com.shigu.passageway.Passageway;
import com.shigu.passageway.exceptions.YeepayException;
import com.shigu.route.PassagewayRoute;
import com.unpay.skb.requests.ModifyRateRequest;
import com.unpay.skb.responses.ModifyRateResponse;
import com.yeepay.skb.biz.FeeSetPartsBuilder;
import com.yeepay.skb.results.FeeSetResult;
import com.yeepay.skb.util.Conts;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

public class RateUtil extends BaseSpringTest{

    @Autowired
    MoPassagewayAccountMapper moPassagewayAccountMapper;

    @Test
    public void modifyYeeRate() throws YeepayException {
        Long userId=872L;
        String rate="0.0041";
        MoPassagewayAccountExample accountExample=new MoPassagewayAccountExample();
        accountExample.createCriteria().andUserIdEqualTo(userId).andPassagewayIdEqualTo(1L);
        accountExample.setStartIndex(0);
        accountExample.setEndIndex(1);
        MoPassagewayAccount updateAccount=new MoPassagewayAccount();
        updateAccount.setRate(rate);
        moPassagewayAccountMapper.updateByExampleSelective(updateAccount,accountExample);
        MoPassagewayAccount yeepay=moPassagewayAccountMapper.selectByConditionList(accountExample).get(0);

        try {
            FeeSetPartsBuilder builder=new FeeSetPartsBuilder();
            builder.setGroupCustomerNumber(Conts.customerNumber);
            builder.setCustomerNumber(yeepay.getAccountCode());
            builder.setProductType("1");
            builder.setRate(rate);
            FeeSetResult result=builder.exec(FeeSetResult.class);
            if (!result.imSuccess()) {
                throw new YeepayException(result.getMessage());
            }
            builder=new FeeSetPartsBuilder();
            builder.setGroupCustomerNumber(Conts.customerNumber);
            builder.setCustomerNumber(yeepay.getAccountCode());
            builder.setProductType("3");
            builder.setRate("2");
            result=builder.exec(FeeSetResult.class);
            if (!result.imSuccess()) {
                throw new YeepayException(result.getMessage());
            }
            builder=new FeeSetPartsBuilder();
            builder.setGroupCustomerNumber(Conts.customerNumber);
            builder.setCustomerNumber(yeepay.getAccountCode());
            builder.setProductType("4");
            builder.setRate("0");
            result=builder.exec(FeeSetResult.class);
            if (!result.imSuccess()) {
                throw new YeepayException(result.getMessage());
            }
            builder=new FeeSetPartsBuilder();
            builder.setGroupCustomerNumber(Conts.customerNumber);
            builder.setCustomerNumber(yeepay.getAccountCode());
            builder.setProductType("5");
            builder.setRate("0");
            result=builder.exec(FeeSetResult.class);
            if (!result.imSuccess()) {
                throw new YeepayException(result.getMessage());
            }
        } catch (IOException e) {
            throw new YeepayException("接口通讯异常");
        }
    }

    @Test
    public void modifyUnpayRate(){
        Long userId=872L;
        String rate="0.0031";
        MoPassagewayAccountExample accountExample=new MoPassagewayAccountExample();
        accountExample.createCriteria().andUserIdEqualTo(userId).andPassagewayIdEqualTo(2L);
        accountExample.setStartIndex(0);
        accountExample.setEndIndex(1);
        MoPassagewayAccount unpay=moPassagewayAccountMapper.selectByConditionList(accountExample).get(0);

        ModifyRateRequest mrr=new ModifyRateRequest();
        mrr.setSubMchId(unpay.getAccountCode());
        String rateStr=MoneyUtil.dealRate(rate).replace("%","");
        mrr.setMerCommisionValue(rateStr);
        mrr.setTxMerCommisionValue(rateStr);
        ModifyRateResponse res=mrr.exec();
        if (res.isSuccess()) {
            MoPassagewayAccount updateAccount=new MoPassagewayAccount();
            updateAccount.setRate(rate);
            moPassagewayAccountMapper.updateByExampleSelective(updateAccount,accountExample);
        }
    }
}
